﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using digitwhu.Models;
using digitwhu.Models.IRepositorys;
namespace digitwhu.Models.Repositorys
{
    public class TeachBuildingRepository : Repository<TeachClassRoom>, ITeachBuildingRepository
    {
        public TeachBuildingRepository(DataContext dbContext)
            : base(dbContext)
        {

        }

        public List<int> GetTeachRoomHavingClass(int buildingId)//查找某栋教学楼的当前的所有上课教室
        {
            //buildingId:教学楼的编号
            //返回值：当前在上课的教室编号列表
            List<int> result = new List<int>();
            var classroom = from c in context.GetTable<TeachClassRoom>()
                            where c.buildingId == buildingId
                            select c.id;
            foreach (int room in classroom)
            {
                //判断该间教室在当前时间是否有课
                //bool b = GetRoomIsHavingClass(room);
               
               // if (b == true)
               // {
                    result.Add(room);   //添加到有课记录中
               // }
            }
            return result;
        }
        public List<TeachMessage> GetTeachMessage(int buildingId)    //获得某栋教室的所有留言
        {
            //buildingId:预获取留言的教室的id
            //返回值：所有留言的对象集合
            List<TeachMessage> result = new List<TeachMessage>();
            var message = from m in context.GetTable<TeachMessage>()
                          where m.buildingId == buildingId
                          select m;
            foreach (TeachMessage temp in message)
            {
                result.Add(temp);   //添加到结果集
            }
            return result;
        }
        public bool AddTeachMessage(int buildingId, int userId, string contents)//给教室留言
        {
            //buildingId:教学楼编号
            //userId：要留言的用户编号
            //contents:留言内容
            //返回值：留言是否成功
            TeachMessage message = new TeachMessage();
            message.buildingId = buildingId;
            if (false == GetBuildingIsTeach(buildingId))
            {
                return false;   //building类型错误
            }
            message.userId = userId;
            message.contents = contents;
            message.dateTime = DateTime.Now;
            try
            {
                context.GetTable<TeachMessage>().InsertOnSubmit(message);
                context.SubmitChanges();    //更新到数据库
                return true;
            }
            catch (Exception e)
            {
                return false;   //数据库异常
            }
        }
        public TeachMessage GetLatestTeachMessage(int buildingId)    //获取某个教室的最新留言
        {
            //buildingId：预获取最新留言的教学楼编号
            //返回值：最新的留言或者Null
            if (GetBuildingIsTeach(buildingId))
            {
                //是教学楼
                var m = from b in context.GetTable<TeachMessage>()
                        where b.buildingId == buildingId
                        select b;
                try
                {
                    TeachMessage result = m.First<TeachMessage>();  //如果没有，则抛出异常
                    foreach (TeachMessage temp in m)
                    {
                        if (DateTime.Compare(temp.dateTime, result.dateTime) > 0)//r1早于r2,r1<r2???未测试
                        {
                            result = temp;
                        }
                    }
                    return result;
                }
                catch (Exception e)
                {
                    return null;    //该教学楼没有留言
                }
            }
            else
            {
                return null;
            }
        }
        #region  私有函数
        private bool GetRoomIsHavingClass(int classroomId)//查询某间教室当前是否有课
        {
            //classroomId:待查询的教室编号
            var classrecord = from r in context.GetTable<TeachCourceRecord>()
                              where r.classRoomId == classroomId
                              select r;
            foreach (TeachCourceRecord r in classrecord)
            {
                //对于每一条上课记录对要检查
                int s = r.startNumberOfClass;
                int e = r.endNumberOfCalss;
                bool temp = GetNowIsInRegion(s, e);
                if (temp == true)
                {
                    return true;    //正在上课
                }
            }
            return false;
        }
        private bool GetNowIsInRegion(int s, int e)  //查询当前时间是否在开课时间s和结课时间的t中间
        {
            //s:开始上课的节数(1~12)
            //e:结束上课的技术(1~14)
            //返回值：true:正在上课，false:没在上课
            //字典里面存的是开始时间，默认上课时间都是45分钟
            Dictionary<int, int> timeTable = new Dictionary<int, int>();
            timeTable.Add(1, 800);//第一节课是8:00开始
            timeTable.Add(2, 850);
            timeTable.Add(3, 950);
            timeTable.Add(4, 1040);
            timeTable.Add(5, 1130);
            timeTable.Add(6, 1315);
            timeTable.Add(7, 1405);
            timeTable.Add(8, 1455);
            timeTable.Add(9, 1545);
            timeTable.Add(10, 1640);
            timeTable.Add(11, 1730);
            timeTable.Add(12, 1900);
            timeTable.Add(13, 1950);
            timeTable.Add(14, 2040);//第十四节课是20:40开始
            if (timeTable.ContainsValue(s) && timeTable.ContainsValue(e))
            {
                int start = timeTable[s];
                int end = timeTable[e];
                if (start < 0 || end < 0)
                {
                    //不存在记录
                    return false;
                }
                int timenow = DateTime.Now.Hour * 100 + DateTime.Now.Minute;
                if (timenow > start && timenow < end + 45)
                {
                    //正在上课
                    return true;
                }
                else
                {
                    //不在上课
                    return false;
                }
            }
            else
            {
                return false;   //字典中不存在记录
            }
        }
        private bool GetBuildingIsTeach(int buildingId) //查询某间栋建筑是不是教学楼
        {
            //buildingId:预查询的建筑物编号
            //返回值：true：是教学楼，false:不是教学楼
            var temp = from b in context.GetTable<building>()
                       where b.id == buildingId && b.type == "teach"
                       select b.id;
            foreach (int id in temp)
            {
                return id == buildingId;    //二次验证
            }
            return false;
        }
        #endregion
    }
}